package com.sgzn.sgpay.web.controller;

import com.sgzn.sgpay.model.request.*;
import com.sgzn.sgpay.model.request.util.PageQueryParam;
import com.sgzn.sgpay.model.response.SgsResultResponse;
import com.sgzn.sgpay.model.response.result.ShopQueryPageResult;
import com.sgzn.sgpay.model.response.vo.ShopDetailVo;
import com.sgzn.sgpay.model.response.vo.ShopVo;
import com.sgzn.sgpay.service.SgsShopService;
import com.sgzn.sgpay.web.controller.base.MerchantBaseController;
import com.sgzn.sgpay.web.handle.AssembleCommonHandle;
import com.sgzn.sgpay.web.request.Impl.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 门店管理控制器
 * @author huangsenguang
 * @version Id: ShopController.java, v 0.1 2018/5/1 11:08 huangsenguang Exp $$
 */
@Slf4j
@Controller
@RequestMapping("/merchant/shop")
public class ShopController extends MerchantBaseController {
    @Resource
    private SgsShopService sgsShopService;

    @Resource
    private AssembleCommonHandle assembleCommonHandle;

    /**
     * 添加门店管理
     * @param request
     * @return
     */
    @RequestMapping("add")
    @ResponseBody
    public SgsResultResponse<String> addShop(ShopAddRequest request) {
        super.setMchInfo(request);
        return this.sgsShopService.addShop(assembleAddRequest(request));
    }

    /**
     * 删除门店管理
     * @param request
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public SgsResultResponse<String> deleteShop(ShopDeleteRequest request) {
        super.setMchInfo(request);
        return this.sgsShopService.deleteShop(assembleDeleteRequest(request));
    }

    /**
     * 查询门店管理的信息
     * @param request
     * @return
     */
    @RequestMapping("query")
    @ResponseBody
    public SgsResultResponse<ShopDetailVo> queryShopInfo(ShopQueryByIdRequest request) {
        super.setMchInfo(request);
        return this.sgsShopService.queryShop(assembleQueryRequest(request));
    }

    /**
     * 分页查询门店管理的信息
     * @param request
     * @return
     */
    @RequestMapping("queryPage")
    @ResponseBody
    public SgsResultResponse<ShopQueryPageResult> queryPageShop(ShopQueryPageRequest request) {
        super.setMchInfo(request);
        return this.sgsShopService.queryPage(assembleQueryPageRequest(request));
    }

    /**
     * 更新门店管理信息
     * @param request
     * @return
     */
    @RequestMapping("update")
    @ResponseBody
    public SgsResultResponse<String> updateShop(ShopUpdateRequest request) {
        super.setMchInfo(request);
        return this.sgsShopService.updateShop(assembleUpdateRequest(request));
    }

    /**
     * 查询商户管理下的门店列表
     * @param request
     * @return
     */
    @RequestMapping("queryList")
    @ResponseBody
    public SgsResultResponse<List<ShopVo>> queryList(ShopQueryListRequest request) {
        super.setMchInfo(request);
        return this.sgsShopService.queryList(assembleQueryListRequest(request));
    }

    private SgsShopAddRequest assembleAddRequest(ShopAddRequest request) {
        SgsShopAddRequest sgsShopAddRequest = new SgsShopAddRequest();
        this.assembleCommonHandle.assemble(request, sgsShopAddRequest);
        sgsShopAddRequest.setShopName(request.getShopName());
        sgsShopAddRequest.setProvince(request.getProvince());
        sgsShopAddRequest.setCity(request.getCity());
        sgsShopAddRequest.setCounty(request.getCounty());
        sgsShopAddRequest.setAddress(request.getAddress());
        sgsShopAddRequest.setIsDefaultAddress(request.getIsDefaultAddress());
        return sgsShopAddRequest;
    }

    private SgsShopDeleteRequest assembleDeleteRequest(ShopDeleteRequest request) {
        SgsShopDeleteRequest sgsShopDeleteRequest = new SgsShopDeleteRequest();
        this.assembleCommonHandle.assemble(request, sgsShopDeleteRequest);
        sgsShopDeleteRequest.setShopId(request.getShopId());
        return sgsShopDeleteRequest;
    }

    private SgsShopQueryRequest assembleQueryRequest(ShopQueryByIdRequest request) {
        SgsShopQueryRequest sgsShopQueryRequest = new SgsShopQueryRequest();
        this.assembleCommonHandle.assemble(request, sgsShopQueryRequest);
        List<String> shopIdList = new ArrayList<>();
        shopIdList.add(request.getShopId());
        sgsShopQueryRequest.setShopId(request.getShopId());
        return sgsShopQueryRequest;
    }

    private SgsShopUpdateRequest assembleUpdateRequest(ShopUpdateRequest request) {
        SgsShopUpdateRequest sgsShopRequest = new SgsShopUpdateRequest();
        this.assembleCommonHandle.assemble(request, sgsShopRequest);
        sgsShopRequest.setShopId(request.getShopId());
        sgsShopRequest.setShopName(request.getShopName());
        sgsShopRequest.setProvince(request.getProvince());
        sgsShopRequest.setCity(request.getCity());
        sgsShopRequest.setCounty(request.getCounty());
        sgsShopRequest.setAddress(request.getAddress());
        sgsShopRequest.setIsDefaultAddress(request.getIsDefaultAddress());
        return sgsShopRequest;
    }

    private SgsShopQueryPageRequest assembleQueryPageRequest(ShopQueryPageRequest request) {
        SgsShopQueryPageRequest sgsShopQueryPageRequest = new SgsShopQueryPageRequest();
        this.assembleCommonHandle.assemble(request, sgsShopQueryPageRequest);
        sgsShopQueryPageRequest.setShopId(request.getShopId());
        sgsShopQueryPageRequest.setShopName(request.getShopName());
        sgsShopQueryPageRequest.setAddress(request.getAddress());
        sgsShopQueryPageRequest.setShopOwner(request.getShopOwner());
        PageQueryParam pageQueryParam = new PageQueryParam();
        pageQueryParam.setPageSize(Integer.parseInt(request.getPageSize()));
        pageQueryParam.setCurrentPage(Integer.parseInt(request.getPageNum()));
        sgsShopQueryPageRequest.setPageQueryParam(pageQueryParam);
        return sgsShopQueryPageRequest;
    }

    private SgsShopQueryListRequest assembleQueryListRequest(ShopQueryListRequest request) {
        SgsShopQueryListRequest target = new SgsShopQueryListRequest();
        this.assembleCommonHandle.assemble(request, target);
        return target;
    }

}

